package me.linx.demo

import android.content.res.ColorStateList
import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_viewpager2.*
import kotlin.math.roundToInt

class ViewPager2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_viewpager2)
        val data = listOf(
            Pair(R.drawable.training_ic_cycling_sel, R.drawable.training_ic_cycling_nor),
            Pair(R.drawable.training_ic_run_sel, R.drawable.training_ic_run_nor),
            Pair(R.drawable.training_ic_walk_sel, R.drawable.training_ic_walk_nor)
        )
        val adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): RecyclerView.ViewHolder {
                val view =
                    LayoutInflater.from(parent.context).inflate(R.layout.item_image, parent, false)

                return object : RecyclerView.ViewHolder(view) {}
            }

            override fun getItemCount(): Int {
                return Integer.MAX_VALUE
            }

            var selected = Integer.MAX_VALUE / 2
                set(value) {
                    if (field != value) {
                        field = value
                        notifyDataSetChanged()
                    }
                }

            override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                val imageView = holder.itemView.findViewById<ImageView>(R.id.iv)
                imageView.setImageResource(data[position % data.size].let {
                    if (selected == position) it.first else it.second
                })
                imageView.setOnClickListener {
                    println("onClick")
                }
            }
        }
        rv.adapter = adapter
        rv.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)

        rv.scrollToPosition(Integer.MAX_VALUE / 2 - 1)
        val helper = PagerSnapHelper()
        helper.attachToRecyclerView(rv)
        rv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                val view = recyclerView.findChildViewUnder(recyclerView.width / 2f, recyclerView.height / 2f)
                view?.let {
                    val holder = recyclerView.findContainingViewHolder(it)
                    holder?.let {
                        adapter.selected = it.adapterPosition
                    }
                }
            }

            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {

            }
        })
        val halfPadding = dp2px(0)
        rv.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                outRect.left = halfPadding
                outRect.right = halfPadding
            }
        })
    }

    val screenWidth: Int
        get() = resources.displayMetrics.widthPixels

    private fun dp2px(dp: Int): Int {
        return (dp * resources.displayMetrics.density).roundToInt()
    }

}